package com.cqc.paoclient.ui.code.codedetail;

import com.cqc.paoclient.http.RetrofitHelper;
import com.cqc.paoclient.model.BaseMsgModel;
import com.cqc.paoclient.model.CodeDetailModel;
import com.cqc.paoclient.util.NetErrorUtil;
import com.flyco.tablayout.widget.MsgView;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${cqc} on 2017/11/28.
 */

public class CodeDetailPresenter extends CodeDetailContract.Presenter {

    @Override
    void getCodeDetail(int codeId) {
        disposable.add(RetrofitHelper.createCodeApi().getCodeDetail(codeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CodeDetailModel>() {
                    @Override
                    public void accept(CodeDetailModel codeDetailModel) throws Exception {
                        mView.getCodeDetailSuccess(codeDetailModel);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        mView.showFail(NetErrorUtil.handleThrowable(throwable));
                    }
                }));
    }

    @Override
    void stow(int codeId) {
        disposable.add(RetrofitHelper.createUserApi().stow(codeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseMsgModel>() {
                    @Override
                    public void accept(BaseMsgModel baseMsgModel) throws Exception {
                        mView.stowSuccess(baseMsgModel);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFail(NetErrorUtil.handleThrowable(throwable));
                    }
                }));
    }
}
